package com.yisingle.study.map.one.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.yisingle.study.map.one.R;

/**
 * @author jikun
 *         Created by jikun on 2018/3/9.
 */

public class MarkerMapFragment extends Fragment {

    private TextureMapView textureMapView;

    private Marker marker;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_marker_map, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        textureMapView = view.findViewById(R.id.textureMapView);
        textureMapView.onCreate(savedInstanceState);
        textureMapView.getMap().setInfoWindowAdapter(new AMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                View infoWindow = LayoutInflater.from(getContext()).inflate(
                        R.layout.map_fast_car_info_window, null);

                setDataOnInfoWindow(infoWindow);
                return infoWindow;

            }

            @Override
            public View getInfoContents(Marker marker) {
                return null;
            }
        });

        addMarker();

    }

    private void setDataOnInfoWindow(View view) {
        view.findViewById(R.id.ll_have_net).setVisibility(View.VISIBLE);
        view.findViewById(R.id.ll_no_net).setVisibility(View.GONE);
        view.findViewById(R.id.ll_left).setVisibility(View.VISIBLE);
        view.findViewById(R.id.iv_loading).setVisibility(View.GONE);
    }

    private void addMarker() {
        MarkerOptions options = new MarkerOptions();
        //对应Marker.setIcon方法  设置Marker的图片
        options.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher));
        //对应Marker.setPosition方法 设置Marker的坐标

        options.title("InfoWindow标题");
        options.snippet("InfoWindow的Snippet");
        //纬度在前 经度在后
        LatLng latLng = new LatLng(30.657505, 104.065692);
        options.position(latLng);
        //然后通过下面的方法来获取Marker Amap类可以通过地图控件TextureMapView.getAmap()方法
        marker = textureMapView.getMap().addMarker(options);
        //显示InfoWindow
        marker.showInfoWindow();
        moveCameraOnMap();
    }

    /**
     * 移动地图到Marker标记点位置的方法。
     */
    private void moveCameraOnMap() {
        LatLng latLng = new LatLng(30.657505, 104.065692);
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLng(latLng);
        textureMapView.getMap().moveCamera(cameraUpdate);
    }


    /**
     * 方法必须重写 来实现TextureMapView的生命周期方法
     */
    @Override

    public void onResume() {
        super.onResume();
        if (null != textureMapView) {
            textureMapView.onResume();
        }

    }

    /**
     * 方法必须重写 来实现TextureMapView的生命周期方法
     */
    @Override
    public void onPause() {
        super.onPause();
        if (null != textureMapView) {
            textureMapView.onPause();
        }
    }

    /**
     * 方法必须重写 来实现TextureMapView的生命周期方法
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (null != textureMapView) {
            textureMapView.onSaveInstanceState(outState);
        }
    }

    /**
     * 方法必须重写 来实现TextureMapView的生命周期方法
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != textureMapView) {
            textureMapView.onDestroy();
        }
        // 别忘了onDestroy 摧毁Marker.
        if (null != marker) {
            marker.destroy();
            marker = null;
        }
    }

    public static MarkerMapFragment newInstance() {

        Bundle args = new Bundle();

        MarkerMapFragment fragment = new MarkerMapFragment();
        fragment.setArguments(args);
        return fragment;
    }

}